package de.baumann.browser.present

import de.baumann.browser.R
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.Like
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IArticleDetailView
import de.baumann.browser.model.NewsModel
import de.baumann.browser.utils.getUserName
import java.util.concurrent.TimeUnit

class ReadArticlePresenter : BasePresenter<IArticleDetailView>() {
    private val newsModel = NewsModel()
    fun queryLiked() {
        this.newsModel.queryLiked(view.getArticleId())
                .subscribe(object : OdinObserver<Like>() {
                    override fun onSuccess(data: Like?) {
                        if (view != null && data != null) {
                            view.setIsLike(data.liked)
                            view.setHasOdin(data.hasOdin == 0)
                        }
                    }
                })
    }


    fun comment(content: String) {
        this.newsModel.comment(getUserName(), view.getArticleId(), content)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        if (view != null)
                            view.commentSuccess()
                    }
                })
    }

    fun like() {
        this.newsModel.like(view.getArticleId(), view.getLikeType())
                .debounce(1, TimeUnit.MINUTES)
                .subscribe(object : OdinObserver<Like>() {
                    override fun onSuccess(data: Like?) {
                        if (view != null) {
                            view.setIsLike(view.getLikeType())
                        }
                    }
                })
    }

    fun addwatchodin() {
        this.newsModel.addwatchodin(view.getArticleId())
                .subscribe(object : OdinObserver<Any>(view, false) {
                    override fun onSuccess(data: Any?) {
                        if (view != null) {
                            view.onError(R.string.Congratulations_on_your_success)
                        }
                    }
                })
    }


    fun complaint(content: String) {
        this.newsModel.complaint(view.getArticleId(), content)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        if (view != null)
                            view.complaintSuccess()
                    }
                })
    }

}